package com.javaclimber.web20fundamentals.meetup.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MeetupNetworking implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final MeetupServiceAsync meetupService = GWT
			.create(MeetupService.class);
	private int action = 1;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		int numMembers = 0;
		final Button networkingButton = new Button("Networking");
		final Button changeEventButton = new Button("Change Group or API Key");
		final Button spinnerButton = new Button("Spinner");
		final TextBox groupIdField = new TextBox();
		final TextBox apiKeyField = new TextBox();

		Timer timer;

		RootPanel.get("networkingButtonContainer").add(networkingButton);
		RootPanel.get("spinnerButtonContainer").add(spinnerButton);
		RootPanel.get("changeEventButtonContainer").add(changeEventButton);

		String apiKey = Window.Location.getParameter("api_key");
		String groupId = Window.Location.getParameter("group_id");

		if(apiKey!=null)
			apiKeyField.setText(apiKey);
		
		if(groupId!=null)
			groupIdField.setText(groupId);
		apiKey=apiKeyField.getText().trim();
		groupId=groupIdField.getText().trim();
		
		if (!("".equals(groupId) && "".equals(apiKey))) {
			RootPanel.get("homet2").setStyleName("visible");
		}
		
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Meetup Id");
		dialogBox.setAnimationEnabled(true);
		final Button submitButton = new Button("Submit");
		submitButton.getElement().setId("submitButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Group Id:</b><br/>"));
		dialogVPanel.add(groupIdField);
		dialogVPanel.add(new HTML("<br/><b>API Key:</b><br/>"));
		dialogVPanel.add(apiKeyField);
		dialogVPanel.add(new HTML("<br/>"));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		dialogVPanel.add(submitButton);
		dialogBox.setWidget(dialogVPanel);

		final MyHandler handler = new MyHandler(groupIdField, apiKeyField,
				dialogBox, meetupService);

		submitButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String groupId = groupIdField.getText().trim();
				String apiKey = apiKeyField.getText().trim();

				RootPanel.get("homet2").setStyleName("visible");
				if ("".equals(groupId) || "".equals(apiKey)) {
					if (dialogBox.isShowing()) {
						Window
								.alert("You must supply both an Event Id and API Key.");
						dialogBox.hide();
						return;
					}
				}

				dialogBox.hide();
				if (action == 1) {
					handler.showMeetupUsers();
				} else if (action == 2) {
					spinnerButton.click();
				} else if (action == 3) {
					changeEventButton.click();
				}
				action = 0;
			}
		});

		changeEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				action = 3;
				dialogBox.show();
				dialogBox.center();
				groupIdField.setFocus(true);
				groupIdField.selectAll();
			}
		});

		timer = new Timer() {

			@Override
			public void run() {
				handler.show2Members();
			}
		};
		handler.setTimer(timer);

		addMenu(networkingButton, spinnerButton, timer);

		networkingButton.addClickHandler(handler);
		spinnerButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				action = 2;

				String groupId = groupIdField.getText().trim();
				String apiKey = apiKeyField.getText().trim();

				if ("".equals(groupId) || "".equals(apiKey)) {
					dialogBox.show();
					dialogBox.center();
					groupIdField.setFocus(true);
					groupIdField.selectAll();
					// action=0;

					return;
				}
				action = 0;
				String url = "http://jfxtras.org/JUGSpinner/JUG_SpinnerWheel.jnlp?arg=apiKey&arg="
						+ apiKey + "&arg=groupId&arg=" + groupId;
				//Window.alert(url);
				Window.open(url, "_blank", "location=no");

			}
		});
	}

	private void addMenu(final Button networkingButton,
			final Button spinnerButton, Timer timer) {
		final Timer fTimer = timer;
		Command cmd = new Command() {
			public void execute() {
				Window.alert("Coming Soon");
			}
		};

		// Make some sub-menus that we will cascade from the top menu.

		MenuBar homeMenu = new MenuBar(true);
		homeMenu.addItem("Home", new Command() {
			public void execute() {
				fTimer.cancel();
				RootPanel.get("home").setStyleName("display");
				RootPanel.get("networking").setStyleName("NoDisplay");

			}
		});

		MenuBar networkingMenu = new MenuBar(true);
		networkingMenu.addItem("Launch", new Command() {
			public void execute() {
				networkingButton.click();
			}
		});
		networkingMenu.addItem("About", cmd);
		networkingMenu.addItem("Google Code Project", new Command() {
			public void execute() {
				Window.open("http://code.google.com/p/meetup-networking/",
						"_blank", "");
			}
		});

		MenuBar spinnerMenu = new MenuBar(true);
		spinnerMenu.addItem("Launch", new Command() {
			public void execute() {
				spinnerButton.click();
			}
		});
		spinnerMenu.addItem("About", cmd);
		spinnerMenu.addItem("Google Code Project", new Command() {
			public void execute() {
				Window.open("http://code.google.com/p/jfxtras/", "_blank", "");
			}
		});

		MenuBar bookMenu = new MenuBar(true);
		bookMenu.addItem("Book Tool Created For", new Command() {
			public void execute() {
				Window.open("http://www.web2-book.com", "_blank", "");
			}
		});

		// Make a new menu bar, adding a few cascading menus to it.
		MenuBar menu = new MenuBar();
		menu.addItem("Home", homeMenu);
		menu.addItem("Networking", networkingMenu);
		menu.addItem("SpinnerWheel", spinnerMenu);
		menu.addItem("Web 2.0 Fundamentals Book", bookMenu);

		// Add it to the root panel.
		RootPanel.get("menuContainer").add(menu);

	}

}
